package com.rowenci.iot_agriculture.service.impl;

import com.rowenci.iot_agriculture.bean.admin.Admin;
import com.rowenci.iot_agriculture.mapper.AdminDao;
import com.rowenci.iot_agriculture.service.api.AdminService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    private AdminDao adminDao;

    @Override
    public Admin findAdmin(String Account, String Password) {
        Admin admin = adminDao.findAdminByAccountAndPassword(Account, Password);
        return admin;
    }

    @Override
    public boolean addAdmin(Admin admin) {
        if (adminDao.findAdminByAccount(admin.getAccount()) == null) {
            if (admin.getAccount() == null || admin.getAccount() == "" ||
                    admin.getPassword() == null || admin.getPassword() == "" ||
                    admin.getSecurityno() == null || admin.getSecurityno() == "") {
                return false;
            } else {
                adminDao.save(admin);
                if (adminDao.findAdminByAccount(admin.getAccount()) == null) {
                    return false;
                } else {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean changePassword(String account, String securityno, String password) {
        adminDao.updatePasswordByAccount(account, securityno, password);
        if (adminDao.findAdminByAccountAndPassword(account, password) == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean changeSecurityNo(String account, String securityno_old, String securityno_new) {
        if (adminDao.findAdminByAccount(account).getSecurityno().equals(securityno_old) == false) {
            return false;
        } else {
            adminDao.updateSecurityno(account, securityno_old, securityno_new);
            Admin admin = adminDao.findAdminByAccount(account);
            if (admin.getSecurityno().equals(securityno_new) == false) {
                return false;
            } else {
                return true;
            }
        }
    }


}
